package com.sangeng.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sangeng.constants.SystemConstants;
import com.sangeng.domain.ResponseResult;
import com.sangeng.domain.vo.CommentVo;
import com.sangeng.domain.vo.PageVo;
import com.sangeng.enums.AppHttpCodeEnum;
import com.sangeng.exception.SystemException;
import com.sangeng.mapper.CommentMapper;
import com.sangeng.service.CommentService;
import com.sangeng.service.UserService;
import com.sangeng.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sangeng.domain.entity.Comment;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 评论表(Comment)表服务实现类
 *
 * @author makejava
 * @since 2023-10-03 15:20:43
 */
@Service("commentService")
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
    @Autowired
    private UserService userService;
    /**
     * 分页查询对应文章所有的评论
     *
     * @param commentType
     * @param articleId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public ResponseResult commentList(String commentType, Long articleId, Integer pageNum, Integer pageSize) {
        //1.查询所有的根评论
        LambdaQueryWrapper<Comment> lqw=new LambdaQueryWrapper<>();
        lqw.eq(SystemConstants.ARTICLE_COMMENT.equals(commentType),
                Comment::getArticleId,articleId);//只有评论类型是文章评论时才设置文章id进行过滤
        lqw.eq(Comment::getRootId, SystemConstants.COMMENT_IS_ROOT);//评论是根评论
        lqw.orderByDesc(Comment::getCreateTime);//根据创建时间进行降序排序
        lqw.eq(Comment::getType,commentType);//选择指定类型的评论
        //2.分页查询
        Page<Comment> page=new Page<>(pageNum,pageSize);
        page(page,lqw);
        //3.进行结果的封装
        //3.1获取所有的根评论
        List<CommentVo> commentVoList=getCommentVoList(page.getRecords());
        //3.2获取所有根评论的所有子评论
        for (CommentVo commentVo : commentVoList) {
           List<CommentVo> children=getChildren(commentVo.getId());
           commentVo.setChildren(children);
        }
        return ResponseResult.okResult(new PageVo(commentVoList,page.getTotal()));
    }


    /**
     * 根据评论id查询所有的子评论
     * @param id
     * @return
     */
    private List<CommentVo> getChildren(Long id) {
        LambdaQueryWrapper<Comment> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Comment::getRootId,id);
        List<Comment> list = list(lqw);
        List<CommentVo> commentVoList = getCommentVoList(list);
        return commentVoList;
    }

    /**
     * 根据commentList进行Vo的结果封装
     * @param records
     * @return
     */
    private List<CommentVo> getCommentVoList(List<Comment> records) {
        //1.属性拷贝
        List<CommentVo> commentVos = BeanCopyUtils.copyBeanList(records, CommentVo.class);
        //2.设置属性username
        for (CommentVo commentVo : commentVos) {
            String nickName = userService.getById(commentVo.getCreateBy()).getNickName();
            commentVo.setUsername(nickName);
            //3.如果commentVo.getToCommentUserId()!=-1,代表该评论有对应的根评论，设置根评论的username
            if(commentVo.getToCommentId()!=SystemConstants.COMMENT_IS_ROOT){
                String nickName1 = userService.getById(commentVo.getToCommentUserId()).getNickName();
                commentVo.setToCommentUserName(nickName1);
            }
        }
        return commentVos;
    }
    /**
     * 发表评论
     * @param comment
     * @return
     */
    @Override
    public ResponseResult addComment(Comment comment) {
        //对提交的评论进行判断
        if(!StringUtils.hasText(comment.getContent())){
            //发表评论但是没有提交内容，抛出异常
            throw new SystemException(AppHttpCodeEnum.ADD_COMMENT);
        }
        //公共字段自动填充
        save(comment);
        return ResponseResult.okResult();
    }

}
